package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.entity.SaleAnalysis;
import pers.cj.pdos.mapper.SaleAnalysisMapper;
import pers.cj.pdos.service.ISaleAnalysisService;
import pers.cj.pdos.utils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2020-03-25
 */
@Service
public class SaleAnalysisServiceImpl extends ServiceImpl<SaleAnalysisMapper, SaleAnalysis> implements ISaleAnalysisService {
    @Autowired
    SaleAnalysisMapper saleAnalysisMapper;

    @Override
    public Map getTotalData(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.getTotalData(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public List<SaleAnalysis> listStoreData(String companyId,Integer page,Integer limit, String storeId, String houseId, Date date1, Date date2){
            return saleAnalysisMapper.listStoreData(companyId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2);
    }

    @Override
    public List<SaleAnalysis> listHouseData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2){
        return saleAnalysisMapper.listHouseData(companyId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2);
    }


    @Override
    public List<SaleAnalysis> listGoodsData(String companyId,
                                            Integer page,
                                            Integer limit,
                                            String storeId,
                                            String houseId,
                                            String customId,
                                            String userId,
                                            Date date1,
                                            Date date2,
                                            String typeId,
                                            Boolean filterZero,
                                            String search) {
        return saleAnalysisMapper.listGoodsData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, customId, userId, date1, date2, typeId, filterZero, search);
    }

    @Override
    public Long countGoodsData(String companyId,
                               String storeId,
                               String houseId,
                               String customId,
                               String userId,
                               Date date1,
                               Date date2,
                               String typeId,
                               Boolean filterZero,
                               String search) {
        return saleAnalysisMapper.countGoodsData(companyId, storeId, houseId, customId, userId, date1, date2, typeId, filterZero, search);
    }

    @Override
    public List<SaleAnalysis> listCustomData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2, String customTypeId, Boolean filterZero, String search) {
        List<SaleAnalysis> list = saleAnalysisMapper.listCustomData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, date1, date2, customTypeId, filterZero, search);

        BigDecimal total = saleAnalysisMapper.getTotalCustom(companyId, storeId, houseId, date1, date2, customTypeId, filterZero, search);
        if (!Utils.isEmpty(list) && total.compareTo(new BigDecimal(0))!=0) {
            List<SaleAnalysis> newList = new ArrayList<>();
            for (SaleAnalysis entity:list) {
                entity.setRatio(entity.getAmount().multiply(new BigDecimal(100)).divide(total,4, RoundingMode.HALF_UP));
                newList.add(entity);
            }
            return newList;
        }

        return list;
    }

    @Override
    public Long countCustomData(String companyId, String storeId, String houseId, Date date1, Date date2, String customTypeId, Boolean filterZero, String search) {
        return saleAnalysisMapper.countCustomData(companyId, storeId, houseId, date1, date2, customTypeId, filterZero, search);
    }

    @Override
    public BigDecimal getTotalCustom(String companyId, String storeId, String houseId, Date date1, Date date2, String customTypeId, Boolean filterZero, String search) {
        return saleAnalysisMapper.getTotalCustom(companyId, storeId, houseId, date1, date2, customTypeId, filterZero, search);
    }

    @Override
    public List<SaleAnalysis> listOrderData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.listOrderData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, date1, date2);
    }

    @Override
    public Long countOrderData(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.countOrderData(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public Map getHighGoods(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.getHighGoods(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public Map getHighCustom(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.getHighCustom(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public List<SaleAnalysis> listGoodsDetailDataByOrder(String companyId, String goodsId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2, String search) {
        return saleAnalysisMapper.listGoodsDetailDataByOrder(companyId, goodsId,  Utils.genPage(page,limit), limit, storeId, houseId, date1, date2,search);
    }

    @Override
    public Long countGoodsDetailDataByOrder(String companyId, String goodsId, String storeId, String houseId, Date date1, Date date2,String search) {
        return saleAnalysisMapper.countGoodsDetailDataByOrder(companyId, goodsId, storeId, houseId, date1, date2,search);
    }

    @Override
    public List<SaleAnalysis> listGoodsDetailDataByCustom(String companyId, String goodsId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2, String search) {
        return saleAnalysisMapper.listGoodsDetailDataByCustom(companyId, goodsId,  Utils.genPage(page,limit), limit, storeId, houseId, date1, date2,search);
    }

    @Override
    public Long countGoodsDetailDataByCustom(String companyId, String goodsId, String storeId, String houseId, Date date1, Date date2, String search) {
        return saleAnalysisMapper.countGoodsDetailDataByCustom(companyId, goodsId, storeId, houseId, date1, date2,search);
    }

    @Override
    public Map getMaxMinDate(String companyId, String goodsId, String customId, String storeId, String houseId) {
        return saleAnalysisMapper.getMaxMinDate(companyId, goodsId, customId, storeId, houseId);
    }

    @Override
    public List<Map> getTotalByYear(String companyId, String goodsId, String customId, String userId, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.getTotalByYear(companyId, goodsId, customId, userId, storeId, houseId, date1, date2);
    }

    @Override
    public List<Map> getTotalByMonth(String companyId, String goodsId, String customId, String userId, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.getTotalByMonth(companyId, goodsId, customId, userId, storeId, houseId, date1, date2);
    }

    @Override
    public List<Map> getTotalByDate(String companyId, String goodsId, String customId, String userId, String storeId, String houseId, Date date1, Date date2) {
        return saleAnalysisMapper.getTotalByDate(companyId, goodsId, customId, userId, storeId, houseId, date1, date2);
    }


}
